{
 "cells": [
  {
   "cell_type": "raw",
   "id": "63ee3f93",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_position: 0\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9316da0d",
   "metadata": {},
   "source": [
    "# Build a Simple LLM Application\n",
    "\n",
    "In this quickstart we'll show you how to build a simple LLM application. This application will translate text from English into another language. This is a relatively simple LLM application - it's just a single LLM call plus some prompting. Still, this is a great way to get started with LangChain - a lot of features can be built with just some prompting and an LLM call!\n",
    "\n",
    "## Concepts\n",
    "\n",
    "Concepts we will cover are:\n",
    "\n",
    "- Using [language models](/docs/concepts/#chat-models)\n",
    "\n",
    "- Using [PromptTemplates](/docs/concepts/#prompt-templates) and [OutputParsers](/docs/concepts/#output-parsers)\n",
    "\n",
    "- [Chaining](/docs/concepts/#langchain-expression-language) a PromptTemplate + LLM + OutputParser using LangChain\n",
    "\n",
    "- Debugging and tracing your application using [LangSmith](/docs/concepts/#langsmith)\n",
    "\n",
    "That's a fair amount to cover! Let's dive in.\n",
    "\n",
    "## Setup\n",
    "\n",
    "### Installation\n",
    "\n",
    "To install LangChain run:\n",
    "\n",
    "```{=mdx}\n",
    "import Npm2Yarn from '@theme/Npm2Yarn';\n",
    "import TabItem from '@theme/TabItem';\n",
    "import CodeBlock from \"@theme/CodeBlock\";\n",
    "\n",
    "<Npm2Yarn>\n",
    "  langchain\n",
    "</Npm2Yarn>\n",
    "```\n",
    "\n",
    "\n",
    "For more details, see our [Installation guide](/docs/how_to/installation/).\n",
    "\n",
    "### LangSmith\n",
    "\n",
    "Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls.\n",
    "As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent.\n",
    "The best way to do this is with [LangSmith](https://smith.langchain.com).\n",
    "\n",
    "After you sign up at the link above, make sure to set your environment variables to start logging traces:\n",
    "\n",
    "```shell\n",
    "export LANGCHAIN_TRACING_V2=\"true\"\n",
    "export LANGCHAIN_API_KEY=\"...\"\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5558ca9",
   "metadata": {},
   "source": [
    "## Detailed walkthrough\n",
    "\n",
    "In this guide we will build an application to translate user input from one language to another.\n",
    "\n",
    "\n",
    "## Using Language Models\n",
    "\n",
    "First up, let's learn how to use a language model by itself. LangChain supports many different language models that you can use interchangably - select the one you want to use below!\n",
    "\n",
    "```{=mdx}\n",
    "import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
    "\n",
    "<ChatModelTabs openaiParams={`model: \"gpt-4\"`} />\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca5642ff",
   "metadata": {},
   "source": [
    "Let's first use the model directly. `ChatModel`s are instances of LangChain \"Runnables\", which means they expose a standard interface for interacting with them. To just simply call the model, we can pass in a list of messages to the `.invoke` method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1b2481f0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage {\n",
       "  lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "  lc_kwargs: {\n",
       "    content: \u001b[32m\"ciao!\"\u001b[39m,\n",
       "    tool_calls: [],\n",
       "    invalid_tool_calls: [],\n",
       "    additional_kwargs: { function_call: \u001b[90mundefined\u001b[39m, tool_calls: \u001b[90mundefined\u001b[39m },\n",
       "    response_metadata: {}\n",
       "  },\n",
       "  lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "  content: \u001b[32m\"ciao!\"\u001b[39m,\n",
       "  name: \u001b[90mundefined\u001b[39m,\n",
       "  additional_kwargs: { function_call: \u001b[90mundefined\u001b[39m, tool_calls: \u001b[90mundefined\u001b[39m },\n",
       "  response_metadata: {\n",
       "    tokenUsage: { completionTokens: \u001b[33m3\u001b[39m, promptTokens: \u001b[33m20\u001b[39m, totalTokens: \u001b[33m23\u001b[39m },\n",
       "    finish_reason: \u001b[32m\"stop\"\u001b[39m\n",
       "  },\n",
       "  tool_calls: [],\n",
       "  invalid_tool_calls: []\n",
       "}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import { HumanMessage, SystemMessage } from \"@langchain/core/messages\"\n",
    "\n",
    "const messages = [\n",
    "    new SystemMessage(\"Translate the following from English into Italian\"),\n",
    "    new HumanMessage(\"hi!\"),\n",
    "]\n",
    "\n",
    "await model.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f83373db",
   "metadata": {},
   "source": [
    "If we've enable LangSmith, we can see that this run is logged to LangSmith, and can see the [LangSmith trace](https://smith.langchain.com/public/45f1a650-38fb-41e1-9b61-becc0684f2ce/r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32bd03ed",
   "metadata": {},
   "source": [
    "## OutputParsers\n",
    "\n",
    "Notice that the response from the model is an `AIMessage`. This contains a string response along with other metadata about the response. Oftentimes we may just want to work with the string response. We can parse out just this response by using a simple output parser.\n",
    "\n",
    "We first import the simple output parser."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d7ae9c58",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { StringOutputParser } from \"@langchain/core/output_parsers\";\n",
    "\n",
    "const parser = new StringOutputParser();"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eaebe33a",
   "metadata": {},
   "source": [
    "One way to use it is to use it by itself. For example, we could save the result of the language model call and then pass it to the parser."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6bacb837",
   "metadata": {},
   "outputs": [],
   "source": [
    "const result = await model.invoke(messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "efb8da87",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[32m\"ciao!\"\u001b[39m"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await parser.invoke(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d508b79d",
   "metadata": {},
   "source": [
    "More commonly, we can \"chain\" the model with this output parser. This means this output parser will get called every time in this chain. This chain takes on the input type of the language model (string or list of message) and returns the output type of the output parser (string).\n",
    "\n",
    "We can easily create the chain using the `.pipe` method. The `.pipe` method is used in LangChain to combine two elements together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9449cfa6",
   "metadata": {},
   "outputs": [],
   "source": [
    "const chain = model.pipe(parser);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3e82f933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[32m\"Ciao!\"\u001b[39m"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await chain.invoke(messages)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd009096",
   "metadata": {},
   "source": [
    "If we now look at LangSmith, we can see that the chain has two steps: first the language model is called, then the result of that is passed to the output parser. We can see the [LangSmith trace](https://smith.langchain.com/public/05bec1c1-fc51-4b2c-ab3b-4b63709e4462/r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ab8da31",
   "metadata": {},
   "source": [
    "## Prompt Templates\n",
    "\n",
    "Right now we are passing a list of messages directly into the language model. Where does this list of messages come from? Usually it constructed from a combination of user input and application logic. This application logic usually takes the raw user input and transforms it into a list of messages ready to pass to the language model. Common transformations include adding a system message or formatting a template with the user input.\n",
    "\n",
    "PromptTemplates are a concept in LangChain designed to assist with this transformation. They take in raw user input and return data (a prompt) that is ready to pass into a language model. \n",
    "\n",
    "Let's create a PromptTemplate here. It will take in two user variables:\n",
    "\n",
    "- `language`: The language to translate text into\n",
    "- `text`: The text to translate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3e73cc20",
   "metadata": {},
   "outputs": [],
   "source": [
    "import { ChatPromptTemplate } from \"@langchain/core/prompts\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e876c2a",
   "metadata": {},
   "source": [
    "First, let's create a string that we will format to be the system message:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "fd75ecde",
   "metadata": {},
   "outputs": [],
   "source": [
    "const systemTemplate = \"Translate the following into {language}:\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fedf6f13",
   "metadata": {},
   "source": [
    "Next, we can create the PromptTemplate. This will be a combination of the `systemTemplate` as well as a simpler template for where the put the text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "88e566f3",
   "metadata": {},
   "outputs": [],
   "source": [
    "const promptTemplate = ChatPromptTemplate.fromMessages(\n",
    "    [\n",
    "        [\"system\", systemTemplate],\n",
    "        [\"user\", \"{text}\"]\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9711ba6",
   "metadata": {},
   "source": [
    "The input to this prompt template is a dictionary. We can play around with this prompt template by itself to see what it does by itself"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f781b3cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatPromptValue {\n",
       "  lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "  lc_kwargs: {\n",
       "    messages: [\n",
       "      SystemMessage {\n",
       "        lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "        lc_kwargs: {\n",
       "          content: \u001b[32m\"Translate the following into italian:\"\u001b[39m,\n",
       "          additional_kwargs: {},\n",
       "          response_metadata: {}\n",
       "        },\n",
       "        lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "        content: \u001b[32m\"Translate the following into italian:\"\u001b[39m,\n",
       "        name: \u001b[90mundefined\u001b[39m,\n",
       "        additional_kwargs: {},\n",
       "        response_metadata: {}\n",
       "      },\n",
       "      HumanMessage {\n",
       "        lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "        lc_kwargs: { content: \u001b[32m\"hi\"\u001b[39m, additional_kwargs: {}, response_metadata: {} },\n",
       "        lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "        content: \u001b[32m\"hi\"\u001b[39m,\n",
       "        name: \u001b[90mundefined\u001b[39m,\n",
       "        additional_kwargs: {},\n",
       "        response_metadata: {}\n",
       "      }\n",
       "    ]\n",
       "  },\n",
       "  lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"prompt_values\"\u001b[39m ],\n",
       "  messages: [\n",
       "    SystemMessage {\n",
       "      lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "      lc_kwargs: {\n",
       "        content: \u001b[32m\"Translate the following into italian:\"\u001b[39m,\n",
       "        additional_kwargs: {},\n",
       "        response_metadata: {}\n",
       "      },\n",
       "      lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "      content: \u001b[32m\"Translate the following into italian:\"\u001b[39m,\n",
       "      name: \u001b[90mundefined\u001b[39m,\n",
       "      additional_kwargs: {},\n",
       "      response_metadata: {}\n",
       "    },\n",
       "    HumanMessage {\n",
       "      lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "      lc_kwargs: { content: \u001b[32m\"hi\"\u001b[39m, additional_kwargs: {}, response_metadata: {} },\n",
       "      lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "      content: \u001b[32m\"hi\"\u001b[39m,\n",
       "      name: \u001b[90mundefined\u001b[39m,\n",
       "      additional_kwargs: {},\n",
       "      response_metadata: {}\n",
       "    }\n",
       "  ]\n",
       "}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "const result = await promptTemplate.invoke({ language: \"italian\", text: \"hi\" })\n",
    "\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a49ba9e",
   "metadata": {},
   "source": [
    "We can see that it returns a `ChatPromptValue` that consists of two messages. If we want to access the messages directly we do:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "2159b619",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\n",
       "  SystemMessage {\n",
       "    lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "    lc_kwargs: {\n",
       "      content: \u001b[32m\"Translate the following into italian:\"\u001b[39m,\n",
       "      additional_kwargs: {},\n",
       "      response_metadata: {}\n",
       "    },\n",
       "    lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "    content: \u001b[32m\"Translate the following into italian:\"\u001b[39m,\n",
       "    name: \u001b[90mundefined\u001b[39m,\n",
       "    additional_kwargs: {},\n",
       "    response_metadata: {}\n",
       "  },\n",
       "  HumanMessage {\n",
       "    lc_serializable: \u001b[33mtrue\u001b[39m,\n",
       "    lc_kwargs: { content: \u001b[32m\"hi\"\u001b[39m, additional_kwargs: {}, response_metadata: {} },\n",
       "    lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"messages\"\u001b[39m ],\n",
       "    content: \u001b[32m\"hi\"\u001b[39m,\n",
       "    name: \u001b[90mundefined\u001b[39m,\n",
       "    additional_kwargs: {},\n",
       "    response_metadata: {}\n",
       "  }\n",
       "]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.toChatMessages()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a4267a8",
   "metadata": {},
   "source": [
    "We can now combine this with the model and the output parser from above. This will chain all three components together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "6c6beb4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "const chain = promptTemplate.pipe(model).pipe(parser);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3e45595a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[32m\"ciao\"\u001b[39m"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await chain.invoke({ language: \"italian\", text: \"hi\" })"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b19cecb",
   "metadata": {},
   "source": [
    "If we take a look at the LangSmith trace, we can see all three components show up in the [LangSmith trace](https://smith.langchain.com/public/cef6edcd-39ed-4c1e-86f7-491a1b611aeb/r)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "befdb168",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "That's it! In this tutorial we've walked through creating our first simple LLM application. We've learned how to work with language models, how to parse their outputs, how to create a prompt template, and how to get great observability into chains you create with LangSmith.\n",
    "\n",
    "This just scratches the surface of what you will want to learn to become a proficient AI Engineer. Luckily - we've got a lot of other resources!\n",
    "\n",
    "For more in-depth tutorials, check out out [Tutorials](/docs/tutorials) section.\n",
    "\n",
    "If you have specific questions on how to accomplish particular tasks, see our [How-To Guides](/docs/how_to) section.\n",
    "\n",
    "For reading up on the core concepts of LangChain, we've got detailed [Conceptual Guides](/docs/concepts)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Deno",
   "language": "typescript",
   "name": "deno"
  },
  "language_info": {
   "file_extension": ".ts",
   "mimetype": "text/x.typescript",
   "name": "typescript",
   "nb_converter": "script",
   "pygments_lexer": "typescript",
   "version": "5.3.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
